<template>
    <!--flex弹性盒子模型，justify-content：主抽 -->
    <div style="display: flex;justify-content: center;">
      <div class="background">
        <img :src="imgSrc" width="100%" height="100%" alt="" />
      </div>
      <el-card style="width: 400px; margin: 1%;">
        <div slot="header" class="clearfix">
          <span class="login-title">修改密码</span>
        </div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="70px" class="demo-ruleForm">
           <el-form-item label="用户名" prop="userId">
            <el-input type="text" v-model="ruleForm.userId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-row>
            <el-button type="primary" @click="resetPwd('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-row>
          <el-row style="float: right">
            <el-button type="text" style="float: right" @click="goBackLogin">返回登录<i class="el-icon-user-solid el-icon--right"></i></el-button>
          </el-row>
        </el-form>
      </el-card>
    </div>
</template>

<script>

import Vue from 'vue'
import { Message } from 'element-ui'
import { resetPwd } from '../../api/index'

Vue.prototype.$message = Message
export default {
  name: 'ResetPwd',
  data: function () {
    var that = this
    var validateUserId = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    var validatePass = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (that.ruleForm.checkPass !== '') {
          that.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== that.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        userId: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        userId: [
          { validator: validateUserId, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      // imgSrc: require('../../assets/img/background.jpg')
      imgSrc: require('../../assets/img/music.jpg')
    }
  },
  methods: {
    resetForm: function (formName) {
      this.$refs[formName].resetFields()
    },
    resetPwd: function (formName) {
      var that = this
      that.$refs[formName].validate((valid) => {
        if (valid) {
          var params = {}
          params.userId = that.ruleForm.userId
          params.passWord = that.ruleForm.passWord
          params.comFirmPassWord = that.ruleForm.checkPass
          resetPwd(params)
            .then(function (res) {
              if (res.code === '000000') {
                that.$message({
                  message: '修改密码成功',
                  type: 'success'
                })
                that.$router.push({path: '/page/login/login'})
              } else {
                that.$message({
                  message: res.message,
                  type: 'warning'
                })
              }
            })
            .catch(function (failResponse) {})
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    goBackLogin: function () {
      this.$router.push({path: '/page/login/login'})
    }
  }
}
</script>

<style scoped>
/* .background{
    width:100%;
    height:100%;  /**宽高100%是为了图片铺满屏幕 */
    /* z-index:-1;
    position: absolute; */
/* } */
.background{
    width:30%;
    z-index: -1;
    margin-top: 1%;
    margin-bottom: 1%;
    float: left;
    position: relative;
}
.login-title{
  font-weight: bold;
  color: #409EFF;
}
</style>
